//
//  YFLineTipView.m
//  HSJCIOS
//
//  Created by zyf on 16/8/10.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "YFLineTipView.h"

@interface YFLineTipView ()

@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIImageView *bgImage;
@property (nonatomic, strong) UILabel *timeLabel;

@end

@implementation YFLineTipView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        self.bgImage = [[UIImageView alloc] init];
        self.bgImage.image = [self resizedImage:@"ChatCurrentTipTLeftBg"];
        [self addSubview:_bgImage];
        
        self.priceLabel = [[UILabel alloc] init];
        self.priceLabel.textColor = UIColorFromRGB(0xff6f6f);
        self.priceLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.priceLabel];
        
        self.timeLabel = [[UILabel alloc] init];
//        self.timeLabel.text = @"2016-08-06 04:50";
        self.timeLabel.textColor = UIColorFromRGB(0x7c7a7a);
        self.timeLabel.font = [UIFont systemFontOfSize:10];
        [self addSubview:self.timeLabel];
    }
    return self;
}

- (UIImage *)resizedImage:(NSString *)name {
    UIImage *image = [UIImage imageNamed:name];
    return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x = 8;
    
    self.priceLabel.frame = CGRectMake(x, 0, self.width - 2 * x, self.height * 0.5);
    self.timeLabel.frame = CGRectMake(x, self.height * 0.5, self.width - x, 12);
    self.bgImage.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)setValueString:(NSString *)valueString
{
    _valueString = valueString;
    
    NSString *string = [NSString stringWithFormat:@"%@元/克",valueString];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
    NSInteger priceStrLength = [string length];
    NSRange range = NSMakeRange(priceStrLength-3, 3);
    [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x3a3a3a) range:range];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:range];
    self.priceLabel.attributedText = str;
    
}

- (void)setTimeString:(NSString *)timeString {
    _timeString = timeString;
    self.timeLabel.text = timeString;
}

- (void)setArrowType:(YFLineTipType)arrowType {
    _arrowType = arrowType;
    
    if (arrowType == YFLineTipTypeLeft) {
        self.bgImage.image = [self resizedImage:@"ChatCurrentTipTRightBg"];
    } else if (arrowType == YFLineTipTypeRight){
        self.bgImage.image = [self resizedImage:@"ChatCurrentTipTLeftBg"];
    }
}

@end
